'''
   该文件用于检测COCO验证集图片为灰度图还是RGB图，这个是因为在验证阶段出现错误
'''

import cv2

import cv2
import os
import numpy as np
from PIL import Image


'''
 利用opencv无法获取灰度图像，该函数得到的结果是不正确的
'''
def get_gray_jpg(root_path):
    data_format = '.jpg'
    for file in os.listdir(root_path):
        if file.endswith(data_format):
            #img = Image.open(file)
            img = cv2.imread(os.path.join(root_path,file))
            _, _, c = np.shape(img)
            if c != 3:                               # 经过检查，没有任何一幅图为Gray图，这样得到的结果是不对的
                print(file)
            if c ==1:
                print(file)
'''
 获取root_path路径下的灰度图像
'''
def get_gray_jpg2(root_path,terminal_display=True):
    '''
    :param root_path:   val图片路径
    :param terminal_display: 是否在终端输出灰度图像
    :return: gray_files: 灰度图像列表
    '''
    import matplotlib.pyplot as plt
    gray_files=list()
    data_format = '.jpg'
    for file in os.listdir(root_path):
        if file.endswith(data_format):
            #img = Image.open(file)
            img = plt.imread(os.path.join(root_path,file))
            c = len(img.shape)
            if c != 3:
                gray_files.append(file)
                if terminal_display:
                    print(file)
    return gray_files
'''
 利用get_gray_jpg2函数对图像进行检测，得到
 000000007888.jpg  删除
 000000205289.jpg  删除
000000209222.jpg   删除
000000024021.jpg
000000061418.jpg
000000353180.jpg
000000431848.jpg
000000130465.jpg
000000141671.jpg
000000274219.jpg   为灰度图像
'''
if __name__ == '__main__':
    get_gray_jpg2('../COCO/val2017')
    #000000007888.jpg
'''
    if 0:
        import matplotlib.pyplot as plt
        img = plt.imread('../COCO/val2017/000000357888.jpg')
        plt.imshow(img)
        plt.show()
    img = cv2.imread('../COCO/val2017/000000209222.jpg')
    cv2.namedWindow("Image")
    cv2.imshow("Image", img)
    cv2.waitKey(0)
   # cv2.imshow(img)
    import matplotlib.pyplot as plt
    img2 = plt.imread('../COCO/val2017/000000209222.jpg')
    img2_ = plt.imread('../COCO/val2017/000000481413.jpg')
    plt.imshow(img2)
    plt.show()
    from PIL import Image
    img3 = Image.open('../COCO/val2017/000000209222.jpg')
    img4 = Image.open('../COCO/val2017/000000481413.jpg')
    plt.imshow(img3)
    plt.show()
    pass
'''
